package com.jlect.swebing.ui.client.button;

import com.jlect.swebing.ui.client.ChangeListener;
import com.jlect.swebing.ui.client.ChangeListenerCollection;
import com.jlect.swebing.ui.client.GComponent;
import com.jlect.swebing.ui.client.Image;
import com.jlect.swebing.util.client.Strings;

/**
 * Default implementation of the button model
 *
 * @author Sergey Kozmin
 * @since 22.11.2007 10:48:48
 */
public class DefaultButtonModelImpl implements ButtonModel {
    private GComponent component;

    private boolean pressed;
    private boolean selected;

    private String text;
    private Image image;

    private ChangeListenerCollection listeners;

    public DefaultButtonModelImpl(GComponent component) {
        this.component = component;
    }

    public void setButtonText(String text) {
        if(!Strings.isEqual(this.text, text)) {
            this.text = text;
            if(listeners != null) {
                listeners.contentChanged(component);
            }
        }
    }

    public String getButtonText() {
        return text;
    }

    public void setButtonImage(Image image) {
        this.image = image;
    }

    public Image getButtonImage() {
        return image;
    }

    public boolean isPressed() {
        return pressed;
    }

    public void setPressed(boolean pressed) {
        if(this.pressed != pressed) {
            this.pressed = pressed;
            if(listeners != null) {
                listeners.stateChanged(component);
            }
        }
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        if(this.selected != selected) {
            this.selected = selected;
            if(listeners != null) {
                listeners.selectionChanged(component);
            }
        }
    }

    public void addChangeListener(ChangeListener listener) {
        if(listeners == null) {
            listeners = new ChangeListenerCollection(); 
        }
        listeners.add(listener);
    }

    public void removeChangeListener(ChangeListener listener) {
        if(listeners != null) {
            listeners.remove(listener);
        }
    }
}
